/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkCityAdvReward;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkCityAdvRewardDao;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkCityAdvGoods;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkCityAdvGoodsDao;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkCityAdvIntegral;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkCityAdvIntegralDao;

/**
 * 城市广告奖励配置Service
 * @author 罗天文
 * @version 2018-03-20
 */
@Service
@Transactional(readOnly = true)
public class HkCityAdvRewardService extends CrudService<HkCityAdvRewardDao, HkCityAdvReward> {

	@Autowired
	private HkCityAdvGoodsDao hkCityAdvGoodsDao;
	@Autowired
	private HkCityAdvIntegralDao hkCityAdvIntegralDao;
	
	public HkCityAdvReward get(String id) {
		HkCityAdvReward hkCityAdvReward = super.get(id);
		hkCityAdvReward.setHkCityAdvGoodsList(hkCityAdvGoodsDao.findList(new HkCityAdvGoods(hkCityAdvReward)));
		hkCityAdvReward.setHkCityAdvIntegralList(hkCityAdvIntegralDao.findList(new HkCityAdvIntegral(hkCityAdvReward)));
		return hkCityAdvReward;
	}
	
	public List<HkCityAdvReward> findList(HkCityAdvReward hkCityAdvReward) {
		return super.findList(hkCityAdvReward);
	}
	
	public Page<HkCityAdvReward> findPage(Page<HkCityAdvReward> page, HkCityAdvReward hkCityAdvReward) {
		return super.findPage(page, hkCityAdvReward);
	}
	
	@Transactional(readOnly = false)
	public void save(HkCityAdvReward hkCityAdvReward) {
		super.save(hkCityAdvReward);
		for (HkCityAdvGoods hkCityAdvGoods : hkCityAdvReward.getHkCityAdvGoodsList()){
			if (hkCityAdvGoods.getId() == null){
				continue;
			}
			if (HkCityAdvGoods.DEL_FLAG_NORMAL.equals(hkCityAdvGoods.getDelFlag())){
				hkCityAdvGoods.setCityId(hkCityAdvReward.getCity().getId());
				hkCityAdvGoods.setCountryId(hkCityAdvReward.getCountry().getId());
				hkCityAdvGoods.setProvinceId(hkCityAdvReward.getProvince().getId());
				hkCityAdvGoods.setEndDate(hkCityAdvReward.getEndDate());
				hkCityAdvGoods.setBeginDate(hkCityAdvReward.getBeginDate());
				hkCityAdvGoods.setCityAdv(hkCityAdvReward.getCityAdv());
				if (StringUtils.isBlank(hkCityAdvGoods.getId())){
					hkCityAdvGoods.setRewardId(hkCityAdvReward);
					hkCityAdvGoods.preInsert();
					hkCityAdvGoodsDao.insert(hkCityAdvGoods);
				}else{
					hkCityAdvGoods.preUpdate();
					hkCityAdvGoodsDao.update(hkCityAdvGoods);
				}
			}else{
				hkCityAdvGoodsDao.delete(hkCityAdvGoods);
			}
		}
		for (HkCityAdvIntegral hkCityAdvIntegral : hkCityAdvReward.getHkCityAdvIntegralList()){
			hkCityAdvIntegral.setCityId(hkCityAdvReward.getCity().getId());
			hkCityAdvIntegral.setCountryId(hkCityAdvReward.getCountry().getId());
			hkCityAdvIntegral.setProvinceId(hkCityAdvReward.getProvince().getId());
			hkCityAdvIntegral.setEndDate(hkCityAdvReward.getEndDate());
			hkCityAdvIntegral.setBeginDate(hkCityAdvReward.getBeginDate());
			hkCityAdvIntegral.setCityAdv(hkCityAdvReward.getCityAdv());
			if (hkCityAdvIntegral.getId() == null){
				continue;
			}
			if (HkCityAdvIntegral.DEL_FLAG_NORMAL.equals(hkCityAdvIntegral.getDelFlag())){
				if (StringUtils.isBlank(hkCityAdvIntegral.getId())){
					hkCityAdvIntegral.setReward(hkCityAdvReward);
					hkCityAdvIntegral.preInsert();
					hkCityAdvIntegralDao.insert(hkCityAdvIntegral);
				}else{
					hkCityAdvIntegral.preUpdate();
					hkCityAdvIntegralDao.update(hkCityAdvIntegral);
				}
			}else{
				hkCityAdvIntegralDao.delete(hkCityAdvIntegral);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkCityAdvReward hkCityAdvReward) {
		super.delete(hkCityAdvReward);
		hkCityAdvGoodsDao.delete(new HkCityAdvGoods(hkCityAdvReward));
		hkCityAdvIntegralDao.delete(new HkCityAdvIntegral(hkCityAdvReward));
	}
	
}